import { upload } from '../services/error';

export const HTTP_ERROR = 'HTTP_ERROR';
export const API_ERROR = 'API_ERROR';
export const CODE_ERROR = 'CODE_ERROR';
export const WECHAT_ERROR = 'WECHAT_ERROR';

export const setErrorHandler = () => {
  window.onerror = (message, source, lineno, colno, error) => {
    upload({
      message,
      source,
      lineno,
      colno,
      error,
    });
  };
  console.error = (error, ...detail) => {
    upload({
      error,
      detail,
    });
  };
};

class ServerError extends Error {
  constructor(type, message, data = {}, api) {
    super(message);
    this.name = `请求${api}-错误-${type}`;
    this.type = type;
    this.data = data;
    this.api = api;
  }
}

export default ServerError;
